package br.com.biavan.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.biavan.model.Resposta;
import br.com.biavan.utils.HibernateUtil;

public class RespostaDAO {
	
	private Session session;		
			
	public boolean inserir(Resposta resposta) {
		boolean resp = false;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(resposta);
			session.getTransaction().commit();
			resp = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resp;
	}

	public boolean atualizar(Resposta resposta) {
		boolean resp = false;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(resposta);
			session.getTransaction().commit();
			resp = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resp;
	}

	public boolean apagar(Resposta resposta) {
		boolean resp = false;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(resposta);
			session.getTransaction().commit();
			resp = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resp;
	}

	public List<Resposta> listar() {
		List<Resposta> respostas = new ArrayList();
		session = Conexao.getSession();
		try {
			session.beginTransaction();
			respostas = session.createCriteria(Resposta.class).list();
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return respostas;
	}
	
	public Resposta getById(int id) {
		Resposta resposta = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			resposta = (Resposta) session.get(Resposta.class, id);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resposta;
	}
	
	public Resposta buscar(Resposta respostaBusca) {
		Resposta resposta = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			
			String select = "from Resposta as u "
					+ "where u.texto = :texto";
			
			Query query = session.createQuery(select)
					.setParameter("texto", respostaBusca.getTexto());
			
			List<?> resultList = query.list();
			for(Object obj : resultList) {
				Object grupo = (Object) obj;
				resposta = (Resposta) grupo;
			}
			
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resposta;
	}
	
}


